******************************************************************************************
** 	Replications file for American Political Science Review 							**
**  I'm a Survivor: Political Dynamics in Bureaucratic Elites' Partisan Identification 	**
** 	Geys, Lægreid, Murdoch and Yackee													**
******************************************************************************************

clear all

global inpath = "E:" // Insert location of the placebo dataset "APSR replication placebo.dta" here

use "$inpath\APSR replication placebo.dta"


********************************************************************************************
**** Create auxiliary dataset including only info on Governor changes by state and year ****
********************************************************************************************

duplicates drop year state, force
sort year state
keep year state GovernorParty
drop if state==.

egen round = group(year)
xtset state round

gen ShiftGovParty=. 
replace ShiftGovParty= GovernorParty - l.GovernorParty

keep year state ShiftGovParty

save "$inpath\temp2.dta", replace


*********************************************************************************
**** PLACEBO CHECK with ideological changes imposed on non-panel respondents ****
**** Random allocation of 7% ideological change each way (cf. 6-7% baseline) ****
*********************************************************************************

clear all

set seed 2024

postfile OUTDATA iteration beta se Tstat using "$inpath\placebo_results.dta", replace

quietly {

* START LOOP
local iterations = 1000							// specify number of iterations
foreach k of numlist 1/`iterations' {		// randomization run number

	clear 
	use "$inpath\APSR replication placebo.dta"
	joinby state year using "$inpath\temp2.dta", unmatched(both)

	keep if PanelID>=1000
	gen random1 = runiform()
	gen random2 = runiform()
	gen ShiftPartyID=1 if random1<0.14 & random2<0.5
	replace ShiftPartyID=-1 if random1<0.14 & random2>=0.5
	replace ShiftPartyID=0 if ShiftPartyID==.

	************* START ANALYSIS ****************
		
	ologit ShiftPartyID ShiftGovParty i.time if (ShiftGovParty~=-1 & ShiftGovParty~=1)

	* SAVE ESTIMATES OF INTEREST AS LOCAL MACROS *
		local  b_`k' =  _b[ShiftGovParty]
		local se_`k' = _se[ShiftGovParty]
		local T_`k' = _b[ShiftGovParty] / _se[ShiftGovParty]
		**********************************************

	post OUTDATA (`k') (`b_`k'') (`se_`k'') (`T_`k'') 
	}

}

postclose OUTDATA


**** Figure A.6: Placebo check on the impact of false positives and measurement error

clear all
use "$inpath\placebo_results.dta"

hist beta, percent xlab(-.3(.1).3) xline(0.245, lcolor(red)) scheme(s1manual) 
graph save "Graph" "$inpath\Placebo_beta_1000.gph", replace

hist Tstat, percent xline(2.95, lcolor(red)) scheme(s1manual)
graph save "Graph" "$inpath\Placebo_T_1000.gph", replace



**************************************************************************
**** CHECK adding non-panel respondents randomly with ShiftPartyID==0 ****
**************************************************************************

clear all

set seed 2024

forvalues x = 5(5)40 {
postfile OUTDATA2 iteration beta se Tstat using "$inpath\selection_results.dta", replace

quietly {

* START LOOP
local iterations = 1000						// specify number of iterations
local select = `x'/100						// Share of non-panel respondents added to dataset
foreach k of numlist 1/`iterations' {		// randomization run number

	clear 
	use "$inpath\APSR replication placebo.dta"
	joinby state year using "$inpath\temp2.dta", unmatched(both)
	
	xtset PanelID time
	
	gen ShiftPartyID=. 
	replace ShiftPartyID= PartyID - l.PartyID

	gen random = runiform() if ShiftPartyID==.
	replace ShiftPartyID=0 if ShiftPartyID==. & random <= `select'

	************* START ANALYSIS ****************
		
	ologit ShiftPartyID ShiftGovParty i.time if (ShiftGovParty~=-1 & ShiftGovParty~=1) & (ShiftPartyID~=-2 & ShiftPartyID~=2)

	* SAVE ESTIMATES OF INTEREST AS LOCAL MACROS *
		local  b_`k' =  _b[ShiftGovParty]
		local se_`k' = _se[ShiftGovParty]
		local T_`k' = _b[ShiftGovParty] / _se[ShiftGovParty]
		**********************************************

	post OUTDATA2 (`k') (`b_`k'') (`se_`k'') (`T_`k'') 
	}

}

postclose OUTDATA2


clear all
use "$inpath\selection_results.dta"

hist beta, start(0.18) width(0.0015) percent xlab(.15(.02).25) xline(0.245, lcolor(red)) scheme(s1manual) title("`x' percent")
graph save "Graph" "$inpath\Selection_beta_`x'.gph", replace

}

**** Figure A.7: Robustness check including non-panel respondents with ShiftPartyID set to 0

graph combine Selection_beta_10.gph Selection_beta_20.gph Selection_beta_30.gph Selection_beta_40.gph, col(2) xcommon ycommon scheme(s1manual)
graph save "Graph" "$inpath\Selection_beta.gph", replace
